//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Packaging;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing.Diagram
{
    class Shape: IOfficeXmlSerializable
    {
        List<Adj> adjs = new List<Adj>();
        OfficeArtExtensionList _OfficeArtExtensionList  = new OfficeArtExtensionList();
        private double _rot;
        private LayoutShapeType _type;
        private int _zOrderOff;
        private bool _hideGeom;
        private bool _lkTxEntry;
        private bool _blipPhldr;
        private BlipData _blipData;

        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read("shape"))
            {
                _rot = c.GetOptionalDouble("rot", 0);
                _type = c.GetOptionalEnum<LayoutShapeType>("type", LayoutShapeType.None);
                string blip = c.GetOptionalString(OfficeConstants.RelationshipNamespace + "blip");
                if (!string.IsNullOrEmpty(blip ))
                {
                    var rel = context.Part.GetRelationship(blip);
                    var blipPart =
    context.Part.Package.GetPart(PackUriHelper.ResolvePartUri(context.Part.Uri, rel.TargetUri));
                    //var dictionary = context.GetData<Dictionary<Uri, object>>();
                   if (false) //\\ if (dictionary != null && dictionary.ContainsKey(blipPart.Uri))
                    {
                       // _blipData = (BlipData)dictionary[blipPart.Uri];
                    }
                    else
                    {
                        this._blipData = new BlipData();
                        _blipData._extension = Path.GetExtension(blipPart.Uri.ToString());
                        var uri = blipPart.Uri.ToString();
                        //\\ blipPart.ContentType;
                        using (var stream = blipPart.GetStream())
                        {
                            _blipData.ReadStream(stream, Path.GetExtension(blipPart.Uri.ToString()));
                        }
//                        if (dictionary != null)
  //                          dictionary.Add(blipPart.Uri, _blipData);
                    }

                    //throw new NotImplementedException();
                }
                _zOrderOff = c.GetOptionalInteger("zOrderOff", 0);
                _hideGeom = c.GetOptionalBoolean("hideGeom", false);
                _lkTxEntry = c.GetOptionalBoolean("lkTxEntry", false);
                _blipPhldr = c.GetOptionalBoolean("blipPhldr", false);
                adjs.Clear();
                adjs.AddRange(c.ReadElements<Adj>("adjLst", "adj"));
                _OfficeArtExtensionList = c.ReadOptionalElement<OfficeArtExtensionList>("extLst");
            }

        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write("shape"))
            {
                c.SetOptionalDouble("rot", _rot, 0);
                c.SetOptionalEnum<LayoutShapeType>("type", _type, LayoutShapeType.None);
                if (_blipData != null)
                    throw new NotImplementedException();
                else
                {
                    c.SetString(OfficeConstants.RelationshipNamespace + "blip", "");
                }
                c.SetOptionalInteger("zOrderOff", _zOrderOff, 0);
                c.SetOptionalBoolean("hideGeom", _hideGeom, false);
                c.SetOptionalBoolean("lkTxEntry", _lkTxEntry, false);
                c.SetOptionalBoolean("blipPhldr", _blipPhldr, false);
                if (adjs.Count > 0)
                {
                    using (var c2 = c.Write("adjLst"))
                    {
                        c2.WriteElements(adjs);
                    }
                }
                c.WriteOptionalElement(_OfficeArtExtensionList);
            }
        }
    }
}